

// console.log(window.location.href);
const param = window.location.href.match(/\?([^&]*([=][^&]*)?&)*id=([^&]*)&?/);
// console.log(param);
if (!param)
{
    alert("作者不存在");
    window.history.back();
}
const userID = param[3];


(async function() {
    "use strict";

    let userInfo = null;

    const getUserResponse = await HTTP.request({
        method: "GET",
        url: `/api/user/id/${userID}`
    });
    if(getUserResponse.data.type === "failed")
    {
        console.error(getUserResponse.data.message);
        alert("错误");
        window.history.back();
        return;
    }
    userInfo = getUserResponse.data.data;
    document.title = "作者——" + userInfo.nickname;

    const description = "鸽子分享网——" + document.title;
    document.querySelector("meta[name='description']").content = description;

    document.querySelector("meta[property='og:title']").content = document.title;
    document.querySelector("meta[property='og:url']").content = `http://cyue.net/pages/author.html?id=${userInfo.id}`;
    document.querySelector("meta[property='og:description']").content= description

    document.querySelector("meta[name='twitter:title']").content= document.title;
    document.querySelector("meta[name='twitter:description']").content = description;
    document.querySelector("meta[name='twitter:creator']").content = `@${userInfo.nickname}`;
    
    
    const { createApp } = Vue;
    createApp({
        mounted() {
            const that = this;
            const avatarIMG = this.$refs.avatarIMG;
            HTTP.request({
                method: "GET",
                url: `/api/avatar/${this.user.avatar_id}`
            }).then(response => {
                const result = response.data;
                avatarIMG.src = result.data.content;
            });
    
            HTTP.request({
                method: "GET",
                url: `/api/article/sort/all`
            }).then(response => {
                const result = response.data;
                that.sortDataSource = result.data;
            });

            HTTP.request({
                method: "GET",
                url: `/api/article/user/id/${this.user.id}`
            }).then(response => {
                const result = response.data;
                for (let index = 0; index < result.data.length; index++) {
                    const element = result.data[index];
                    if (!that.articleDataSource[element.sort])
                        that.articleDataSource[element.sort] = [];
                    that.articleDataSource[element.sort].push(element);
                }
            });
        },
    
        data() {
            return {
                user: userInfo,
                sortDataSource: [],
                articleDataSource: {}
            };
        },
    
    
        methods: {
    
        }
    
    }).mount("#app");
    
    
})();

